package com.bdcom.comp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.text.DecimalFormat;

import javax.swing.JPanel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bdcom.util.PlannerUtil;
/**
 * 比例标签
 * @author Administrator
 *
 */
@Component
public class ScaleLabelPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String m=PlannerUtil.getString("m");
	
	@Autowired
	private CanvasPanel canvasPanel;
	
	public ScaleLabelPanel() {
		super();
		// TODO Auto-generated constructor stub
		setPreferredSize(new Dimension(0,30));
	}
	
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		
		int panelWidth = this.getWidth();
		
		int panelHeight = this.getHeight();
		
		double value=0;
		
		value=canvasPanel.getScale();
		
		String txt = "1cm:"+new DecimalFormat("#0.00").format(value) + m;
		
		FontMetrics metrics =g.getFontMetrics();
		
		int size=metrics.stringWidth(txt);
		
		int txty=panelHeight/2;
		
		int liney=txty;
		
		int txtx=0,linex1=0,linex2=0;

		int maxLength=size+(int) PlannerUtil.DPCM+10;

		if(maxLength<panelWidth)
		{
			Graphics2D g2d=(Graphics2D) g;
			
			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			
			g2d.setColor(Color.black);

			txtx=(panelWidth-maxLength)/2;
			
			linex1=txtx+size+10;
			
			linex2=linex1+(int) PlannerUtil.DPCM;
			
			g2d.drawString(txt, txtx, txty);
			
			g2d.drawLine(linex1, liney, linex2, liney);
			
			g2d.drawLine(linex1, liney, linex1, liney - 5);
			
			g2d.drawLine(linex2, liney, linex2, liney - 5);
			
		}
	}

}
